/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2017 All Rights Reserved.
 */
package com.citytsm.gateway.sdk.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 *
 * @author James
 * @version $Id: TimeStampUtil.java, v 0.1 2017年06月29日 下午8:09 James Exp $
 */
public class TimeStampUtil {
    public static Long getTimeStamp() {
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        String timestamp = String.valueOf(ts.getTime());
        return Long.valueOf(String.valueOf(timestamp.substring(0, timestamp.length() - 3)));
    }

    private static final SimpleDateFormat[] FORMART_LIST = {
            //new SimpleDateFormat("yyyy-MM", Locale.CHINA),
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA),
            new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA),
            new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA),
            new SimpleDateFormat("yyyy-MM", Locale.CHINA)

    };
    //static{
    //    for(SimpleDateFormat f:FORMART_LIST)
    //    {
    //        f.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
    //    }
    //}


    /**
     * 时间格式化，精确到秒.
     * @return
     */
    public static String formart2Second(Date time) {

        if (time == null) {
            return null;
        }
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        } catch (Exception e) {
            //do Nothing
        }
        throw new RuntimeException("暂无法识别该日期格式");
    }

    /**
     * 时间格式化，精确到秒.
     * @return
     */
    public static Date addMonth(Date time, int month) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        cal.add(Calendar.MONTH, month);
        return cal.getTime();
    }

    public static Date getFirstDayOfMonth(Date time){
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        cal.set(Calendar.MINUTE, 0);
        //将秒至0
        cal.set(Calendar.SECOND,1);
        return cal.getTime();
    }
}

